var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pagesize:15,
    pageIndex: 1,
    recordList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    device_id:0,
    state:0,
    currentTab:0,
    tabs_state:[0,1,2],
    departType:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.device_id = parseInt(options.id)
  },
  
  onChange:function(event) {
    console.log('onChange',event)
    var index = event.detail.index
    var state = this.data.tabs_state[index]
    this.data.state = state;
    this.data.pageIndex = 1;
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });
    this.onHttpInit()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });
    this.onHttpInit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/device/welding/record/list', {
      device_id:that.data.device_id,
      state:that.data.state,
      page_index:that.data.pageIndex,
      page_size:that.data.pagesize
    }, function (res) {
      console.log("上报记录响应数据", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;
        that.data.departType = res.data.data.departType;
        if (res.data.totalcount > 0) {
          var records = res.data.data.records;
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          
          that.setData({
            departType: that.data.departType,
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goCheckRecord(e){
    console.log('goCheckRecord',e);
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/welding/check/index?action=check&id=' + item.id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshrecordList()
  },
  refreshrecordList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      recordList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})